<template>
    <view>
        <!-- pages/roomList/roomList.wxml -->
        <view class="tabs">
            <view class="item">
                <van-dropdown-menu :active-color="mainColor">
                    <van-dropdown-item :value="storeId" :options="stores" @change="storeDropdown" />
                </van-dropdown-menu>
            </view>
            <view class="item">
                <view class="btn red" @tap="openStoreDoor">开大门</view>
            </view>
        </view>
        <view class="container">
            <view class="list">
                <view class="item" v-for="(item, index) in doorList" :key="index">
                    <view class="left">
                        <view class="info">
                            <text class="name">
                                {{ item.roomName }}（{{ item.type === 1 ? '小包' : item.type === 2 ? '中包' : item.type === 3 ? '大包' : item.type === 4 ? '豪包' : '商务包' }}）
                            </text>
                        </view>
                        <view class="info">
                            <label>门店：</label>
                            <text>{{ item.storeName }}</text>
                        </view>
                        <view class="info">
                            <label>状态：</label>
                            <text>{{ item.status == 1 ? '空闲' : item.status == 2 ? '待清洁' : item.status == 3 ? '使用中' : item.status == 4 ? '已预约' : '禁用' }}</text>
                        </view>
                        <block v-if="item.status == 2">
                            <view class="tip color-attention" v-if="item.endTime">待清洁 | {{ item.startTime }}开始</view>
                        </block>
                        <block v-if="item.status == 3">
                            <view class="tip color-attention" v-if="item.endTime">使用中 | {{ item.endTime }}结束</view>
                        </block>
                        <block v-if="item.status == 4">
                            <view class="tip color-attention" v-if="item.endTime">已预订 | {{ item.startTime }}开始</view>
                        </block>
                    </view>

                    <block v-if="userinfo.userType != 14 || (userinfo.userType == 14 && item.clearOpenDoor)">
                        <view class="right">
                            <view class="btn red" @tap="roomOp" :data-room="item">控制功能</view>
                        </view>
                    </block>
                </view>
            </view>
            <view class="buttom">- 加载完毕 -</view>
        </view>
        <!-- 房间控制弹出 -->
        <van-dialog use-slot title="房间控制" :show="showRoomOp" confirm-button-text="关闭" @confirm="closeRoomOp">
            <view class="dialog">
                <view class="item">
                    <view class="name">【{{ roomName }}】</view>
                </view>
                <view class="item">
                    <view class="btn red" @tap="openDoor">开电</view>
                    <view class="btn red" @tap="closeDoor">关电</view>
                    <view class="btn waring" @tap="testYunlaba">测试喇叭</view>
                </view>
                <view class="item" v-if="userinfo.userType != 14">
                    <view class="btn waring" @tap="disableRoom">启用禁用</view>
                    <view class="btn waring" @tap="clearAndFinish">保洁完成</view>
                    <view class="btn red" @tap="toPlaceOrder">代下单</view>
                </view>
            </view>
        </van-dialog>
    </view>
</template>

<script>
// pages/roomList/roomList.js
const app = getApp();
var http = require('../../../utils/http');
var lock = require('../../../utils/lock.js');
export default {
    data() {
        return {
            storeId: '',
            //列表搜索门店id
            stores: [],
            userinfo: {
                userType: 0
            },
            doorList: [],
            pageNo: 1,
            pageSize: 10,
            isIpx: app.globalData.isIpx ? true : false,
            foldIndex: -1,
            showRoomOp: false,
            roomItem: {},
            mainColor: app.globalData.mainColor,
            roomName: '',
            roomId: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        this.getXiaLaListAdmin();
        this.getuserinfo();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getDoorList();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        foldTap(e) {
            console.log(e);
            const { target: { dataset = {} } = {} } = e;
            this.setData({
                foldIndex: this.foldIndex === dataset.index ? -1 : dataset.index
            });
        },

        //管理员获取门店下拉列表数据
        getXiaLaListAdmin: function (e) {
            var that = this;
            //if (app.globalData.isLogin)
            {
                http.request(
                    '/member/store/getStoreList',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('下拉门店数据===');
                        console.info(info);
                        if (info.code == 0) {
                            let stores = [];
                            info.data.map((it) => {
                                stores.push({
                                    text: it.key,
                                    value: it.value
                                });
                            });
                            stores.unshift({
                                text: '全部门店',
                                value: ''
                            });
                            that.setData({
                                stores: stores,
                                storeId: stores[0].value
                            });
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        // 获取房间列表
        getDoorList: function () {
            let that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/store/getRoomInfoList?storeId=' + that.storeId,
                    '1',
                    'post',
                    {
                        // "storeId": that.data.storeId
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            that.setData({
                                doorList: info.data
                            });
                        } else {
                            uni.showModal({
                                content: '请求服务异常，请稍后重试',
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        //门店下拉菜单发生变化
        storeDropdown(event) {
            //console.log(event)
            this.stores.map((it) => {
                if (it.value === event.detail) {
                    this.setData({
                        storeId: it.value
                    });
                }
            });
            this.getDoorList('refresh');
        },

        // 开房间门
        openDoor: function (e) {
            let that = this;
            let roomId = that.roomItem.roomId;
            console.log(roomId);
            uni.showModal({
                title: '提示',
                content: '确定打开房间门和电源吗',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            // that.openBlueDoor();
                            http.request(
                                '/member/store/openRoomDoor/' + roomId,
                                '1',
                                'post',
                                {
                                    roomId: roomId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '开门成功',
                                            icon: 'success'
                                        });
                                    } else {
                                        uni.showModal({
                                            content: '请求服务异常，请稍后重试',
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        // 关房间门
        closeDoor: function () {
            let that = this;
            let roomId = that.roomItem.roomId;
            uni.showModal({
                title: '提示',
                content: '确定关闭房间门和电源吗',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/closeRoomDoor/' + roomId,
                                '1',
                                'post',
                                {
                                    roomId: roomId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '关门成功',
                                            icon: 'success'
                                        });
                                    } else {
                                        uni.showModal({
                                            content: '请求服务异常，请稍后重试',
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        getuserinfo: function () {
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/user/get',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('我的信息===');
                        console.info(info);
                        if (info.code == 0) {
                            that.setData({
                                userinfo: info.data
                            });
                        }
                    },
                    function fail(info) {}
                );
            } else {
                //console.log('未登录失败！')
            }
        },

        // 打开大门
        openStoreDoor: function () {
            var that = this;
            if (!that.storeId) {
                uni.showModal({
                    content: '请选择需要开门的门店',
                    showCancel: false
                });
            } else {
                http.request(
                    '/member/store/openStoreDoor/' + that.storeId,
                    '1',
                    'post',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info(info);
                        if (info.code == 0) {
                            uni.showToast({
                                title: '操作成功',
                                icon: 'success'
                            });
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
            // console.info(id);
        },

        roomOp: function (e) {
            let room = e.currentTarget.dataset.room;
            this.setData({
                roomItem: room,
                showRoomOp: true,
                roomId: room.roomId,
                roomName: room.roomName
            });
        },

        closeRoomOp: function () {
            this.setData({
                roomItem: {},
                showRoomOp: false
            });
        },

        testYunlaba: function (e) {
            let that = this;
            let roomId = that.roomItem.roomId;
            uni.showModal({
                title: '提示',
                content: '房间喇叭将播放预设内容，确定操作吗？',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/testYunlaba/' + roomId,
                                '1',
                                'post',
                                {
                                    roomId: roomId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '操作成功',
                                            icon: 'success'
                                        });
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        // 清洁并结单
        clearAndFinish: function (e) {
            let that = this;
            let roomId = that.roomItem.roomId;
            uni.showModal({
                title: '注意提示',
                content: '注意！！！房间状态将变为空闲！并立即关电！如果有进行中的订单，订单将会被结束！请谨慎确认房间当前状态后再操作！！！',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/clearAndFinish/' + roomId,
                                '1',
                                'get',
                                {},
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    // console.info('返回111===');
                                    // console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '操作成功',
                                            icon: 'success'
                                        });
                                        that.getDoorList();
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        disableRoom: function (e) {
            let that = this;
            let roomId = that.roomItem.roomId;
            uni.showModal({
                title: '提示',
                content: '确定修改房间状态吗？',
                complete: (res) => {
                    if (res.cancel) {
                    }
                    if (res.confirm) {
                        if (app.globalData.isLogin) {
                            http.request(
                                '/member/store/disableRoom/' + roomId,
                                '1',
                                'post',
                                {
                                    roomId: roomId
                                },
                                app.globalData.userDatatoken.accessToken,
                                '',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        uni.showToast({
                                            title: '操作成功',
                                            icon: 'success'
                                        });
                                        that.getDoorList();
                                    } else {
                                        uni.showModal({
                                            content: info.msg,
                                            showCancel: false
                                        });
                                    }
                                },
                                function fail(info) {}
                            );
                        }
                    }
                }
            });
        },

        // 代下单
        toPlaceOrder: function () {
            this.setData({
                showRoomOp: false
            });
            uni.navigateTo({
                url: `/pages/placeOrder/placeOrder?id=${this.roomId}&roomName=${this.roomName}`
            });
        }
    }
};
</script>
<style>
@import './roomList.css';
</style>
